<?php
/**
 * 缩略图对象
 * @author yagas<yagas@163.com>
 * @version 0.41
 * @package CFormModel
 */
class Thumb extends CFormModel {
	
	private $_origin;
	
	public function __construct( $source ) {
		$this->_origin = imagecreatefromstring( $source );
	}
	
	/**
	 * 对图片进行绽放
	 * @return 返回图片GD
	 */
	protected function & _zoom( $width, $height ) {
		$originWidth   = imagesx( $this->_origin );
		$originHeight  = imagesy( $this->_origin );
		$originPercent = $originWidth / $originHeight;
		if( is_null($height) ) {
			$newWidth  = $width;
			$newHeight = $width / $originPercent;
		}
		else {
			if( $originWidth > $originHeight ) {
				$newWidth  = $width;
				$newHeight = $width * $originPercent;
			}
			else {
				$newHeight = $width;
				$newWidth  = $width * $originPercent;
			}
		}
		$im = imagecreatetruecolor( $newWidth, $newHeight );
		imagecopyresampled($im, $this->_origin, 0, 0, 0, 0, $newWidth, $newHeight, $originWidth, $originHeight);
		return $im;
	}
	
	public function zoom( $width, $height=null ) {
		$im = $this->_zoom( $width, $height );
		ob_start();
		imagejpeg( $im );
		$binData = ob_get_contents();
		ob_end_clean();
		return $binData;
	}
}